package com.example.new5.views;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.new5.R;
import com.example.new5.entity.Book;
import com.example.new5.utils.JsonUtils;
import com.google.gson.Gson;

public class BookFragment extends Fragment {

    private TextView titleTextView,catalogTextView,tagsTextView,sub1TextView,sub2TextView;
    private TextView readingTextView,onlineTextView,bytimeTextView,jingdong,dangdang,suning;
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book, container, false);
        titleTextView = view.findViewById(R.id.title);
        catalogTextView = view.findViewById(R.id.catalog);
        tagsTextView = view.findViewById(R.id.tags);
        sub1TextView = view.findViewById(R.id.sub1);
        sub2TextView = view.findViewById(R.id.sub2);
        readingTextView = view.findViewById(R.id.reading);
        onlineTextView = view.findViewById(R.id.online);
        bytimeTextView = view.findViewById(R.id.bytime);
        imageView = view.findViewById(R.id.image);
        jingdong = view.findViewById(R.id.jingdong);
        dangdang = view.findViewById(R.id.dangdang);
        suning = view.findViewById(R.id.suning);

        String title=getValueByKey("title");
        titleTextView.setText(title);

        String catalog=getValueByKey("catalog");
        catalogTextView.setText(catalog);

        String tags=getValueByKey("tags");
        tagsTextView.setText(tags);

        String sub1=getValueByKey("sub1");
        sub2TextView.setText(sub1);

        String sub2=getValueByKey("sub2");
        sub2TextView.setText(sub2);

        String reading=getValueByKey("reading");
        readingTextView.setText(reading);

//        String online=getValueByKey("online");
//        onlineTextView.setText(online);

        jingdong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://book.jd.com/10483893.html";
                openWebPage(url);
            }
        });
        dangdang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://product.dangdang.com/product.aspx?product_id=21020821";
                openWebPage(url);
            }
        });
        suning.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://www.suning.com/emall/prd_10052_22001_-7_1006212_.html";
                openWebPage(url);
            }
        });

        String bytime=getValueByKey("bytime");
        bytimeTextView.setText(bytime);

        String image=getValueByKey("image");
        imageView.setTag(image);

        // 使用Glide加载图片
        Glide.with(this)
                .load("https://apis.juhe.cn/goodbook/img/379cdafe13f92d62e99388182a6d08ec.jpg")
                .into(imageView);
        return view;
    }
    private void openWebPage(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
            startActivity(intent);
        } else {
            // 处理没有可用浏览器的情况
            Toast.makeText(getActivity(), "没有可用浏览器", Toast.LENGTH_SHORT).show();
        }
    }

    private String getValueByKey(String name) {
        String jsonStr = getJson("book.json");
        String value = JsonUtils.getKeyRecursively(jsonStr, name);
        Log.d("asdf", "getValueByKey value ==== " + value);
        return value;
    }
    private String getJson(String name) {
        String json = JsonUtils.getJsonFromAssets(getContext(), name);
        Log.d("asdf", "getJson = " + json);
        return json;
    }
}